/*
* 本案例功能
* 接入个人服务器，发布信息，订阅信息，
* 实现温度模块 光敏模块数据上传 用手机app和个人物联网平台实现红外遥控器功能 可通过天猫精灵开关电视
* D4 温度上传端口
* A0 光照数据
* D2 红外发射模块口
* 上传主题 getMessage
* 订阅主题 air_control
*/
#include <DHT.h>
//定义针脚
#define DHTPIN 2 
//定义类型，DHT11或者其它
#define DHTTYPE DHT11
//进行初始设置 
DHT dht(DHTPIN, DHTTYPE);
#include <ESP8266WebServer.h>
#include <WiFiManager.h> 
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#include <DS18B20.h>
#define BUS D4//温湿度读取端口
#define PRODUCT_ID    "TOP" //产品名（）
#define API_KEY    "tx6WM==zmW21Z2pt4susBRlHMuY="//产品密钥
#define DEVICE_ID "air_device"//设备名
#define TOPIC     "air_control"//订阅主题
//电视遥控器对应信号 （本例所用品牌为康佳）
uint16_t autoMatic[243] = {8466, 4128,  534, 562,  532, 1610,  536, 1610,  534, 558,  536, 1608,  536, 560,  534, 1612,  534, 564,  514, 1630,  536, 1608,  536, 560,  534, 1610,  534, 558,  534, 1610,  536, 1610,  536, 562,  536, 558,  534, 558,  536, 558,  534, 558,  536, 558,  534, 558,  536, 558,  536, 562,  534, 558,  536, 1610,  538, 558,  534, 558,  536, 556,  536, 556,  510, 584,  536, 564,  534, 558,  534, 558,  534, 558,  536, 558,  532, 1610,  536, 558,  534, 558,  536, 564,  536, 1610,  536, 556,  536, 556,  536, 556,  534, 560,  534, 558,  534, 558,  532, 566,  510, 584,  534, 1610,  534, 558,  536, 556,  536, 556,  538, 558,  532, 1610,  538, 562,  536, 556,  536, 558,  536, 558,  534, 560,  532, 558,  534, 560,  534, 558,  534, 566,  534, 558,  536, 558,  532, 560,  536, 558,  534, 560,  534, 558,  534, 558,  536, 562,  534, 558,  534, 558,  536, 558,  536, 556,  534, 560,  534, 558,  534, 560,  534, 564,  536, 556,  534, 560,  534, 558,  534, 558,  536, 556,  534, 558,  534, 560,  536, 562,  534, 558,  536, 556,  534, 558,  536, 556,  534, 558,  508, 584,  510, 582,  536, 562,  536, 558,  534, 558,  534, 558,  536, 558,  534, 558,  536, 558,  536, 560,  534, 564,  534, 560,  534, 558,  534, 558,  536, 556,  532, 562,  534, 560,  530, 560,  534, 564,  534, 560,  534, 1610,  534, 1612,  534, 558,  510, 584,  534, 1610,  536, 560,  534, 560,  536};  // MIRAGE

uint16_t temperature_up[243] = {8450, 4128,  538, 554,  536, 1608,  538, 1608,  512, 580,  536, 1608,  538, 556,  536, 1608,  514, 586,  536, 1608,  538, 556,  538, 1606,  538, 556,  536, 1608,  540, 1606,  536, 1608,  538, 562,  536, 556,  538, 554,  538, 556,  538, 556,  538, 556,  538, 556,  536, 556,  538, 562,  536, 558,  536, 556,  538, 556,  536, 558,  536, 556,  512, 580,  536, 556,  538, 562,  536, 554,  538, 556,  538, 556,  536, 556,  538, 556,  536, 1608,  538, 556,  536, 564,  536, 556,  538, 556,  512, 580,  512, 580,  538, 556,  536, 558,  536, 556,  538, 560,  540, 552,  536, 556,  536, 558,  538, 554,  538, 556,  536, 556,  536, 558,  536, 562,  538, 556,  538, 554,  536, 556,  538, 556,  536, 556,  538, 554,  538, 556,  536, 562,  536, 556,  538, 556,  536, 556,  536, 556,  538, 556,  538, 556,  538, 554,  538, 562,  536, 556,  538, 554,  538, 556,  536, 556,  536, 556,  538, 556,  536, 556,  538, 560,  538, 554,  514, 580,  540, 554,  536, 556,  538, 556,  536, 556,  538, 556,  538, 558,  538, 556,  536, 558,  536, 558,  536, 554,  538, 556,  512, 580,  536, 556,  536, 562,  538, 554,  538, 556,  512, 580,  536, 556,  538, 556,  536, 556,  538, 554,  538, 562,  536, 558,  538, 556,  538, 554,  538, 554,  536, 558,  536, 556,  536, 556,  536, 562,  536, 1608,  538, 556,  538, 554,  538, 1608,  538, 1608,  538, 556,  540, 552,  540, 556,  538};  // MIRAGE

uint16_t model[243] = {8450, 4124,  538, 554,  538, 1606,  542, 1602,  542, 554,  538, 1604,  542, 554,  538, 1608,  538, 560,  538, 556,  538, 556,  538, 1604,  540, 554,  540, 1606,  540, 1606,  540, 1606,  538, 562,  538, 554,  540, 554,  538, 552,  540, 554,  538, 554,  538, 554,  540, 554,  540, 558,  540, 554,  540, 554,  538, 554,  540, 552,  538, 554,  542, 552,  538, 554,  538, 558,  542, 552,  540, 556,  538, 554,  540, 554,  538, 554,  540, 1604,  540, 554,  540, 558,  538, 554,  538, 554,  540, 552,  542, 552,  540, 552,  538, 556,  538, 552,  540, 560,  542, 550,  540, 554,  542, 552,  538, 554,  538, 554,  538, 554,  540, 554,  536, 560,  540, 552,  540, 554,  538, 554,  540, 554,  538, 554,  540, 552,  538, 554,  538, 562,  538, 554,  540, 554,  538, 554,  540, 554,  536, 556,  540, 552,  540, 552,  540, 558,  538, 554,  538, 556,  538, 554,  538, 554,  540, 552,  540, 556,  536, 554,  540, 560,  540, 552,  540, 554,  536, 554,  538, 554,  540, 554,  538, 556,  536, 554,  540, 558,  538, 554,  540, 554,  538, 554,  538, 554,  540, 554,  538, 556,  538, 554,  540, 558,  540, 552,  540, 554,  540, 552,  540, 554,  538, 554,  542, 550,  540, 552,  540, 560,  538, 554,  540, 552,  540, 554,  540, 552,  538, 556,  536, 556,  538, 552,  538, 562,  538, 554,  540, 554,  538, 554,  540, 1606,  540, 1604,  542, 554,  538, 556,  538, 560,  540};  // MIRAGE

uint16_t temperature_down[243] = {8444, 4130,  534, 562,  532, 1610,  536, 1610,  508, 584,  534, 1612,  528, 566,  534, 1612,  508, 590,  508, 584,  532, 560,  534, 1610,  534, 560,  536, 1608,  510, 1636,  534, 1612,  534, 564,  534, 560,  534, 558,  534, 560,  534, 560,  534, 558,  532, 562,  532, 560,  510, 590,  534, 558,  532, 562,  534, 558,  536, 558,  508, 584,  534, 558,  534, 560,  534, 564,  534, 560,  534, 558,  534, 558,  534, 560,  508, 584,  532, 1612,  534, 560,  534, 564,  510, 584,  532, 560,  508, 584,  536, 558,  508, 584,  534, 558,  510, 586,  506, 592,  508, 586,  532, 560,  534, 560,  534, 558,  534, 558,  532, 562,  532, 560,  534, 566,  534, 558,  534, 560,  508, 586,  534, 558,  508, 584,  534, 560,  534, 558,  510, 590,  534, 558,  534, 558,  534, 560,  534, 560,  534, 558,  534, 560,  534, 560,  534, 566,  534, 558,  534, 560,  532, 562,  506, 584,  534, 560,  532, 560,  534, 560,  534, 562,  536, 560,  508, 584,  536, 558,  534, 560,  534, 560,  534, 560,  532, 558,  532, 568,  534, 558,  508, 584,  532, 562,  532, 560,  534, 558,  534, 558,  532, 560,  534, 564,  508, 584,  534, 558,  532, 558,  536, 558,  534, 558,  532, 560,  534, 560,  532, 564,  534, 560,  532, 560,  510, 582,  534, 560,  536, 556,  534, 558,  534, 560,  532, 564,  534, 560,  532, 560,  508, 586,  534, 1610,  534, 1612,  534, 560,  532, 560,  532, 562,  532};  // MIRAGE

uint16_t power[243] = {8446, 4128,  508, 586,  532, 1610,  512, 1634,  510, 586,  532, 1612,  534, 560,  534, 1610,  534, 566,  534, 558,  508, 584,  510, 1636,  536, 558,  532, 1614,  534, 1610,  510, 1638,  534, 564,  534, 560,  506, 584,  510, 584,  536, 558,  512, 582,  534, 560,  508, 584,  534, 566,  508, 584,  534, 558,  534, 558,  536, 556,  534, 560,  534, 558,  534, 558,  510, 590,  534, 1612,  536, 1610,  508, 586,  534, 560,  532, 1612,  508, 586,  532, 560,  506, 592,  508, 584,  532, 560,  534, 560,  534, 558,  534, 560,  508, 586,  530, 560,  534, 566,  534, 558,  532, 560,  534, 558,  534, 560,  508, 586,  508, 584,  536, 1610,  536, 562,  534, 560,  534, 560,  532, 560,  532, 560,  534, 560,  532, 562,  532, 560,  536, 564,  532, 560,  532, 560,  534, 558,  532, 560,  508, 586,  534, 560,  534, 560,  532, 566,  534, 558,  532, 562,  532, 560,  510, 582,  534, 558,  508, 582,  536, 558,  510, 592,  532, 558,  534, 558,  532, 560,  508, 584,  536, 558,  534, 558,  510, 584,  532, 568,  532, 560,  508, 584,  536, 558,  532, 560,  510, 582,  510, 584,  508, 584,  532, 568,  534, 558,  532, 560,  508, 584,  534, 560,  508, 584,  534, 560,  506, 586,  508, 590,  534, 560,  532, 560,  508, 584,  508, 584,  534, 558,  536, 558,  534, 560,  534, 566,  534, 560,  534, 1610,  510, 1636,  508, 1638,  534, 1610,  510, 584,  534, 560,  508, 590,  532};  // MIRAGE 开关

uint16_t wind_up[243] = {8448, 4130,  536, 558,  536, 1608,  536, 1610,  534, 560,  536, 1608,  536, 558,  534, 1608,  512, 590,  532, 560,  536, 558,  534, 1610,  510, 582,  536, 1608,  534, 1612,  536, 1608,  510, 590,  534, 560,  534, 558,  534, 560,  534, 558,  534, 560,  534, 560,  536, 558,  534, 562,  512, 582,  534, 560,  534, 558,  534, 560,  532, 560,  532, 560,  534, 560,  534, 564,  534, 558,  536, 1608,  536, 556,  536, 560,  532, 558,  536, 1610,  536, 558,  534, 566,  536, 558,  534, 558,  536, 556,  536, 558,  508, 584,  536, 556,  534, 560,  534, 566,  534, 560,  532, 560,  510, 582,  534, 558,  536, 556,  510, 584,  534, 560,  534, 564,  536, 558,  534, 558,  534, 558,  510, 582,  536, 558,  510, 584,  506, 586,  510, 590,  508, 582,  536, 558,  534, 558,  536, 558,  534, 558,  534, 558,  536, 558,  532, 566,  536, 560,  532, 560,  534, 558,  534, 558,  534, 560,  534, 558,  512, 582,  532, 566,  534, 558,  534, 560,  536, 556,  532, 562,  532, 560,  534, 560,  508, 586,  534, 564,  508, 584,  508, 586,  510, 582,  510, 584,  506, 586,  534, 558,  510, 584,  534, 566,  532, 558,  534, 558,  508, 584,  536, 560,  532, 560,  508, 586,  534, 558,  534, 566,  510, 582,  536, 558,  508, 586,  532, 558,  536, 558,  534, 560,  508, 584,  534, 564,  534, 558,  510, 1634,  536, 558,  536, 1608,  510, 1636,  536, 558,  534, 560,  532, 564,  534};  // MIRAGE

uint16_t wind_down[243] = {8454, 4124,  536, 556,  536, 1608,  538, 1608,  540, 556,  534, 1610,  536, 556,  538, 1606,  536, 564,  536, 556,  538, 556,  512, 1630,  538, 560,  534, 1608,  538, 1608,  538, 1606,  538, 564,  534, 556,  514, 582,  534, 558,  536, 556,  536, 558,  536, 556,  536, 556,  512, 586,  536, 556,  538, 556,  512, 580,  534, 558,  536, 556,  536, 556,  536, 556,  536, 562,  538, 1608,  538, 556,  514, 580,  536, 556,  536, 556,  536, 1608,  536, 558,  536, 562,  538, 556,  536, 558,  534, 556,  514, 580,  536, 556,  536, 556,  538, 556,  512, 586,  538, 554,  538, 556,  536, 554,  538, 558,  534, 556,  538, 556,  536, 556,  536, 560,  536, 558,  536, 556,  536, 558,  536, 556,  536, 558,  536, 556,  536, 558,  534, 564,  538, 556,  536, 556,  512, 582,  536, 556,  536, 556,  536, 558,  538, 556,  512, 586,  538, 554,  536, 558,  512, 580,  536, 556,  520, 572,  538, 556,  536, 554,  538, 562,  536, 556,  512, 580,  514, 580,  536, 556,  536, 558,  536, 558,  536, 556,  536, 562,  536, 554,  536, 558,  536, 556,  538, 556,  538, 556,  536, 558,  536, 556,  536, 562,  514, 580,  538, 556,  538, 556,  536, 556,  512, 580,  536, 558,  538, 556,  536, 562,  538, 556,  536, 556,  538, 554,  536, 556,  512, 582,  534, 558,  536, 554,  538, 562,  536, 1608,  538, 554,  514, 580,  538, 1606,  536, 1612,  536, 556,  538, 556,  538, 558,  538};  // MIRAGE

uint16_t wind_ud[243] = {8422, 4154,  536, 558,  508, 1636,  536, 1610,  532, 562,  534, 1610,  508, 584,  536, 1610,  538, 562,  536, 558,  508, 586,  508, 1636,  536, 558,  506, 1636,  510, 1638,  534, 1610,  510, 590,  534, 560,  508, 582,  534, 560,  532, 560,  532, 560,  534, 560,  532, 560,  508, 590,  508, 584,  536, 558,  534, 558,  536, 556,  532, 560,  532, 560,  534, 560,  508, 590,  508, 1638,  510, 584,  532, 558,  514, 580,  508, 586,  508, 1636,  536, 558,  532, 566,  532, 560,  510, 1636,  510, 584,  534, 560,  532, 562,  530, 560,  534, 558,  532, 566,  508, 586,  532, 558,  526, 568,  532, 560,  534, 560,  532, 562,  506, 586,  508, 590,  510, 584,  508, 584,  532, 560,  534, 560,  534, 558,  534, 558,  532, 562,  534, 564,  510, 584,  534, 560,  534, 558,  534, 558,  534, 558,  532, 560,  534, 560,  506, 592,  532, 560,  532, 560,  536, 558,  510, 584,  508, 586,  530, 560,  534, 558,  532, 566,  508, 584,  510, 584,  534, 560,  534, 558,  534, 558,  510, 582,  510, 584,  536, 562,  534, 558,  534, 560,  508, 586,  534, 558,  532, 560,  508, 584,  534, 560,  532, 564,  536, 558,  532, 560,  534, 560,  532, 560,  508, 584,  510, 584,  532, 560,  510, 590,  506, 586,  508, 584,  532, 560,  508, 584,  534, 558,  534, 560,  532, 560,  534, 564,  534, 1612,  536, 1610,  534, 560,  532, 1610,  534, 1612,  536, 558,  536, 556,  512, 588,  532};  // MIRAGE

uint16_t wind_lr[243] = {8450, 4126,  538, 556,  536, 1608,  538, 1606,  540, 556,  538, 1606,  538, 556,  538, 1606,  538, 562,  536, 556,  538, 556,  538, 1606,  538, 556,  538, 1606,  540, 1606,  540, 1608,  538, 560,  538, 556,  536, 556,  536, 554,  538, 554,  538, 556,  538, 554,  538, 556,  536, 562,  538, 556,  536, 558,  534, 554,  540, 556,  536, 558,  536, 554,  538, 554,  538, 562,  540, 1604,  540, 554,  536, 558,  536, 554,  538, 556,  534, 1610,  538, 554,  536, 562,  538, 1608,  538, 556,  536, 556,  538, 554,  538, 554,  538, 554,  538, 554,  540, 560,  538, 556,  538, 556,  536, 558,  536, 556,  538, 554,  540, 554,  536, 556,  538, 560,  538, 554,  538, 554,  538, 554,  538, 556,  538, 554,  538, 554,  538, 556,  536, 564,  534, 556,  538, 554,  538, 554,  538, 554,  538, 556,  540, 554,  536, 554,  540, 560,  538, 554,  540, 552,  540, 554,  538, 554,  538, 556,  538, 556,  536, 556,  538, 560,  538, 554,  538, 558,  538, 554,  538, 556,  538, 554,  538, 556,  538, 556,  538, 560,  538, 556,  536, 556,  536, 556,  538, 556,  538, 554,  536, 558,  538, 554,  538, 560,  538, 554,  538, 554,  540, 554,  538, 556,  538, 554,  538, 556,  538, 554,  538, 562,  538, 552,  540, 556,  538, 554,  538, 554,  538, 556,  538, 556,  538, 556,  538, 560,  538, 554,  538, 1606,  540, 554,  536, 1608,  540, 1606,  538, 556,  536, 556,  536, 560,  536};  // MIRAGE

//uint16_t sound_down[75] = {3040, 2898,  548, 1436,  548, 1438,  552, 1434,  550, 1436,  550, 1434,  550, 1436,  550, 2434,  550, 1436,  548, 1438,  548, 1438,  548, 1438,  550, 2432,  550, 1438,  548, 1438,  550, 2436,  548, 1436,  550, 3900,  546, 23986,  3004, 2932,  542, 1444,  540, 1446,  544, 1444,  542, 1444,  538, 1448,  542, 1444,  540, 2444,  542, 1442,  518, 1468,  542, 1444,  542, 1444,  542, 2442,  518, 1470,  542, 1444,  542, 2442,  542, 1446,  542, 3908,  514};  // UNKNOWN 31A9CF7B

IRsend irsend(4);
double tempertureRead;
WiFiClient wifiClient;
Ticker ticker;
int count = 0; //ticker1控制 数据上传下发的间隔时间（s）
PubSubClient mqttClient(wifiClient);
const char* mqttServer = "********";//服务器地址
const uint16_t mqttPort = 1883;//mqtt接口端口
char msgJson[75];//存json下发信息数据
char msg_buf[200];//存json上传数据及标识位
double brightNum;

//接入bigiot
String DEVICEID="***"; // bigiot设备编号   
String  APIKEY = "e620f4fab"; // bigiot设备密码
unsigned long lastCheckInTime = 0; //记录上次报到时间
const unsigned long postingInterval = 40000; // 每隔40秒向服务器报到一次
const char* host = "www.bigiot.net";
const int httpPort = 8181;
WiFiClient client;
//
void setup() {
// 初始化串口
Serial.begin(9600);
WiFiManager wifiManager;
// 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称
wifiManager.autoConnect("空调WiFi设置");
Serial.print("WiFi Connected!");
irsend.begin();
pinMode(A0, INPUT);
dht.begin(); //DHT开始工作
/*WiFi.mode(WIFI_STA);
WiFi.begin(ssidd, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}*/
mqttClient.setServer(mqttServer, mqttPort);
// 设置MQTT订阅回调函数
mqttClient.setCallback(receiveCallback);
connectMQTTServer();
ticker.attach(1, addCount);
}

void loop() {
if (mqttClient.connected()) { // 如果开发板成功连接服务器
// 每隔2秒钟发布一次信息
// 保持心跳 若电机正在运转，暂时不发信息（由于mcu没有多线程，不能同时运转电机和上传下发数据）
if (count >= 5)
{
  int h = dht.readHumidity();//读湿度
  double t = dht.readTemperature();//读温度，默认为摄氏度
  pubMsgAndroid(h,t);
  //pubMQTTmsg();
  count = 0;
}
mqttClient.loop();
} else {                  // 如果开发板未能成功连接服务器
connectMQTTServer();    // 则尝试连接服务器
// WiFi.begin(ssidd, password);
}
if (!client.connected()) {
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
delay(5000);
return;
}
}
if(millis() - lastCheckInTime > postingInterval || lastCheckInTime==0) {
checkIn();
}
// Read all the lines of the reply from server and print them to Serial
if (client.available()) {
String inputString = client.readStringUntil('\n');
inputString.trim();
Serial.println(inputString);
int len = inputString.length()+1;
if(inputString.startsWith("{") && inputString.endsWith("}")){
//char jsonString[len];
//inputString.toCharArray(jsonString,len);
        
StaticJsonDocument<192> doc;
deserializeJson(doc, inputString);
String get_c = doc["C"]; // "play"
if(get_c=="play"){
irsend.sendRaw(power, 243, 38); 
}
else if(get_c=="stop"){
irsend.sendRaw(power, 243, 38); 
}

}
}

}
//连接mqtt服务器
void connectMQTTServer() {
String clientId = DEVICE_ID;
String productId = PRODUCT_ID;
String apiKey = API_KEY;
// 连接MQTT服务器
if (mqttClient.connect(clientId.c_str(), productId.c_str(), apiKey.c_str())) {
Serial.println("MQTT Server Connected.");
Serial.println("Server Address: ");
Serial.println(mqttServer);
Serial.println("ClientId:");
Serial.println(clientId);
subscribeTopic(); // 订阅指定主题
} else {
Serial.print("MQTT Server Connect Failed. Client State:");
Serial.println(mqttClient.state());
delay(1000);
}
}
// 订阅指定主题
void subscribeTopic() {
// 这么做是为确保不同设备使用同一个MQTT服务器测试消息订阅时，所订阅的主题名称不同
String topicString = TOPIC;
char subTopic[topicString.length() + 1];
strcpy(subTopic, topicString.c_str());

// 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
if (mqttClient.subscribe(subTopic)) {
Serial.println("Subscrib Topic:");
Serial.println(subTopic);
} else {
Serial.print("Subscribe Fail...");
}
}
//获取下发指令topic 指定主题 payload 下发信息，以字节存储 length 下发信息长度
void receiveCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message Received [");
Serial.print(topic);
Serial.print("] ");
String receiveMessage;
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
receiveMessage += (char)payload[i];
}
Serial.println("----" + receiveMessage + "----");
Serial.print("Message Length(Bytes) ");
Serial.println(length);
///
StaticJsonDocument<256> doc;
deserializeJson(doc, receiveMessage);

int model1 = doc["model"]; // 1
int temperature_up1 = doc["temperature_up"]; // 1
int autoMatic1 = doc["autoMatic"]; // 1
int temperature_down1 = doc["temperature_down"]; // 1
int wind_up1 = doc["wind_up"]; // 1
int wind_down1 = doc["wind_down"]; // 1
int wind_ud1 = doc["wind_ud"]; // 1
int wind_lr1 = doc["wind_lr"]; // 1
int power1 = doc["power"]; // 1
//int homes1 = doc["home"]; // 1
receiveMessage = "";
if (model1==1)
{
irsend.sendRaw(model, 243, 38); 
} else if (temperature_up1==1) {
irsend.sendRaw(temperature_up, 243, 38); 
} else if (autoMatic1==1) {
irsend.sendRaw(autoMatic, 243, 38); 
} else if (temperature_down1==1) {
irsend.sendRaw(temperature_down, 243, 38); 
} else if (wind_up1==1) {
irsend.sendRaw(wind_up, 243, 38); 
} else if (wind_down1==1) {
irsend.sendRaw(wind_down, 243, 38); 
} else if (wind_ud1==1) {
irsend.sendRaw(wind_ud, 243, 38); 
} else if (power1==1) {
irsend.sendRaw(power, 243, 38); 
} else if (wind_lr1==1) {
irsend.sendRaw(wind_lr, 243, 38); 
} 

}
//对指定主题上传信息
void pubMsgAndroid(int h,double t) {
String topicString = "getMessage";
char publishTopic[topicString.length() + 1];
strcpy(publishTopic, topicString.c_str());
//json数据转换为数组
DynamicJsonDocument doc(64);
doc["temperature"] = t;
brightNum=analogRead(A0);

doc["bright"] = (int)((100-brightNum/1024*100)*100+0.5)/100.0;
doc["hum"] = h;
serializeJson(doc, Serial);
String jsonCode;
serializeJson(doc, jsonCode);
Serial.print("json Code: "); Serial.println(jsonCode);
String messageString = jsonCode;
char publishMsg[messageString.length() + 1];
strcpy(publishMsg, messageString.c_str());
int json_len = strlen(publishMsg);
mqttClient.publish(publishTopic, publishMsg, json_len);
}
void addCount() {
count++;
}

void checkIn() {
    String msg = "{\"M\":\"checkin\",\"ID\":\"" + DEVICEID + "\",\"K\":\"" + APIKEY + "\"}\n";
    client.print(msg);
    lastCheckInTime = millis(); 
}
void sayToClient(String client_id, String content){
  String msg = "{\"M\":\"say\",\"ID\":\"" + client_id + "\",\"C\":\"" + content + "\"}\n";
  client.print(msg);
  lastCheckInTime = millis();
}
